package com.sino.operation.util;

import com.sino.operation.dto.Dto;
import com.sino.operation.entity.BizOperateLog;
import com.sino.operation.entity.BizTranslist;
import com.sino.operation.entity.SysAbMessage;
import com.sino.operation.param.ChargeSubTradeParam;
import com.sino.operation.param.out.EscReceiveByCounterToAccount;
import com.sino.operation.param.out.EscReciveByCounter;
import com.sino.operation.peripheral.esc.request.NetHandle;
import com.sino.operation.vo.TaskChoiceParam;
import com.sino.operation.param.inner.TaskCompleteParam;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @ClassName ReflectExecuteUtil
 * @Description 反射执行方法发
 * @Author 北陈
 * @Date 2021/10/15 14:10
 * @Version 1.0
 */
@Slf4j
public class ReflectExecuteUtil {

    public static void executeCharge(String className, String serviceName, ChargeSubTradeParam chargeSubTradeParam){
        try {
            Object obj = Class.forName(className).newInstance();
            Class clzz = obj.getClass();
            Method method = clzz.getDeclaredMethod(serviceName, ChargeSubTradeParam.class);
            Map map = (Map) method.invoke(obj, chargeSubTradeParam);

        } catch (Exception e) {
            log.error("调用类：" + className + "-方法：" + serviceName + "出现错误", e.getMessage());
        }
    }

    public static Map getResult(String className, String methodName, Map<String, Object> param) {

        try {
            Object obj = Class.forName("Excel_" + className).newInstance();
            Class clzz = obj.getClass();
            Method method = clzz.getDeclaredMethod("execute" + methodName, Map.class);
            Map map = (Map) method.invoke(obj, param);
            return map;
        } catch (Exception e) {
            log.error("调用类：" + className + "-方法：" + methodName + "出现错误", e.getMessage());
        }
        return null;
    }

    public static Dto getTaskList(String className, NetHandle netHandle) {
        try {
            Object obj = Class.forName("Excel_" + className).newInstance();
            Class clzz = obj.getClass();
            Method method = clzz.getDeclaredMethod("executeGetTaskList", NetHandle.class);
            Dto dto = (Dto) method.invoke(obj, netHandle);
            return dto;
        } catch (Exception e) {
            log.error("调用类：" + className + "-方法executeGetTaskList出现错误", e.getMessage());
        }
        return null;
    }

    public static PersistenceUtil execute(String className, String methodName, Map varMap, BizTranslist bizTranslist, SysAbMessage sysAbMessage, BizOperateLog bizOperateLog, Object registerTable, Object receiveParam) throws Exception {
        PersistenceUtil persistenceUtil = new PersistenceUtil();
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(Map.class, BizTranslist.class, SysAbMessage.class,BizOperateLog.class, Object.class, Object.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(varMap, bizTranslist, sysAbMessage,bizOperateLog, registerTable, receiveParam);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                persistenceUtil = (PersistenceUtil) method.invoke(o, new Object[]{});
                break;
            }
        }
        return persistenceUtil;
    }

    public static Object executeGetTask(String className, String methodName, String journalNo,SysAbMessage sysAbMessage) throws Exception {
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(String.class,SysAbMessage.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(journalNo,sysAbMessage);
        Object result=null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                 result= method.invoke(o, new Object[]{});
                break;
            }
        }
        return result;
    }

    public static PersistenceUtil executeStartAccount(String className, String methodName, Map varMap, BizTranslist bizTranslist, SysAbMessage sysAbMessage, EscReceiveByCounterToAccount escReceiveByCounterToAccount) throws Exception {
        PersistenceUtil persistenceUtil = new PersistenceUtil();
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(Map.class, BizTranslist.class, SysAbMessage.class, EscReceiveByCounterToAccount.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(varMap, bizTranslist, sysAbMessage, escReceiveByCounterToAccount);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                persistenceUtil = (PersistenceUtil) method.invoke(o, new Object[]{});
                break;
            }
        }
        return persistenceUtil;
    }

    public static PersistenceUtil executeStart(String className, String methodName, Map varMap, BizTranslist bizTranslist, SysAbMessage sysAbMessage, EscReciveByCounter escReciveByCounter) throws Exception {
        PersistenceUtil persistenceUtil = new PersistenceUtil();
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(Map.class, BizTranslist.class, SysAbMessage.class, EscReciveByCounter.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(varMap, bizTranslist, sysAbMessage, escReciveByCounter);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                persistenceUtil = (PersistenceUtil) method.invoke(o, new Object[]{});
                break;
            }
        }
        return persistenceUtil;
    }

    public static PersistenceUtil executeComplete(String className, String methodName, Map varMap, BizTranslist bizTranslist, SysAbMessage sysAbMessage, TaskCompleteParam taskCompleteParam) throws Exception {
        PersistenceUtil persistenceUtil = new PersistenceUtil();
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(Map.class, BizTranslist.class, SysAbMessage.class, TaskCompleteParam.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(varMap, bizTranslist, sysAbMessage, taskCompleteParam);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                persistenceUtil = (PersistenceUtil) method.invoke(o, new Object[]{});
                break;
            }
        }
        return persistenceUtil;
    }


    public static PersistenceUtil executeOther(String className, String methodName, Map varMap, BizTranslist bizTranslist, SysAbMessage sysAbMessage, TaskChoiceParam taskChoiceParam) throws Exception {
        PersistenceUtil persistenceUtil = new PersistenceUtil();
        Class<?> aClass = Class.forName("com.sino.operation.service.business." + className);
        Constructor<?> constructor = aClass.getConstructor(Map.class, BizTranslist.class, SysAbMessage.class, TaskChoiceParam.class);
        Method[] methods = aClass.getMethods();
        Object o = constructor.newInstance(varMap, bizTranslist, sysAbMessage, taskChoiceParam);
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                persistenceUtil = (PersistenceUtil) method.invoke(o, new Object[]{});
                break;
            }
        }
        return persistenceUtil;
    }
}
